library AmmoAcquire initializer init requires LegacySystem, AmmoReload,  AmmoAPI


	private function AcquireAmmoCondition takes nothing returns boolean
		return GetSpellAbilityId()=='A059'
	endfunction

	private function AcquireAmmo takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local player p = GetOwningPlayer(u)
		local integer i = GetConvertedPlayerId(p)
		local integer id = GetUnitTypeId(u)
		local boolean empty = false
		local string plusammo
		
		if GetUnitTypeId(u)=='H000' then
			if GetPrimaryAmmoRounds(p)<=2350 then
				if GetPrimaryAmmoRounds(p)==0 then
					call UnitAddAbility(u,'A044')
				endif
				
				call AddPrimaryAmmo(u, 0, 150)
				set udg_Reloading[i] = 0
				call UnitRemoveAbility(u,'A02H')
				set plusammo = "+150"
			else
				call IssueImmediateOrder(GetTriggerUnit(),"holdposition")
				call DisplayTimedTextToPlayer(p,0.0,0.0,2.0, "You have reached Fuel Capacity." )
				set plusammo = " "
			endif
			call AddFadingTextTag(plusammo,GetUnitX(u),GetUnitY(u),255,220,0,200)
			
		elseif IsUnitType(u,UNIT_TYPE_HERO)==true then
			if GetPrimaryAmmoMagazines(p)<40 then
			
				if GetPrimaryAmmoMagazines(p)==0 and udg_Reloading[i]==3 then
					set empty =  true
				endif
				
				if GetUnitTypeId(u)=='H005' then
					call AddPrimaryAmmo(u, 2, 0)
					set plusammo = "+2"
				else
					call AddPrimaryAmmo(u, 3, 0)
					set plusammo = "+3"
				endif
				call AddFadingTextTag(plusammo,GetUnitX(u),GetUnitY(u),255,220,0,200)
				
				if empty then
					call ConditionalTriggerExecute( AmmoAutoReloadTrigger )
				endif

				if getPlayerDataInteger(p,"pistol_ammo")<=0 and AmmoSystemMagazines[i]>0 and getPlayerDataBoolean(p,"pistol_reload")==false then
					call setPlayerDataBoolean(p,"pistol_reload",true)
					call PolledWait(2.)
					call ShiftMoveSpeed(udg_Hero[i],1.4)
					
					call AddPrimaryAmmo(u, -1, 0)
					
					set bj_meleeNearestMineDist = GetUnitX(u)
					set bj_lastTransmissionDuration = GetUnitY(u)
					set udg_soundpath = "war3mapImported\\usp_clipin.wav"
					call PlaySoundPath3D.execute()
					call PolledWait(1.)

					if (getPlayerDataInteger(p,"pistol_type")==1) then
						call UnitAddAbility(u,'A07P')
						call setPlayerDataInteger(p,"pistol_ammo",8)
					elseif (getPlayerDataInteger(p,"pistol_type")==0) then
						call UnitAddAbility(u,'A06H')
						call setPlayerDataInteger(p,"pistol_ammo",8)
					elseif (getPlayerDataInteger(p,"pistol_type")==2) then
						call UnitAddAbility(u,'A08T')
						call setPlayerDataInteger(p,"pistol_ammo",15)
					endif

					if udg_Experience[id]>65000 then
						call IncUnitAbilityLevel(u,'A06H') //Colt
						call IncUnitAbilityLevel(u,'A07P') //Desert Eagle
					endif

					call setPlayerDataBoolean(p,"pistol_reload",false)
				endif
			else
				//set plusammo = " "
				call IssueImmediateOrder(GetTriggerUnit(),"holdposition")
				call DisplayTimedTextToPlayer(p,0.0,0.0,2.0, "You cannot carry any additional magazines." )
				//call AddFadingTextTag(udg_mystring,GetUnitX(u),GetUnitY(u),255,220,0,200)
			endif
		else
			//set udg_mystring = " "
			call IssueImmediateOrder(u,"holdposition")
			call DisplayTimedTextToPlayer(p,0.0,0.0,2.0, "Only Heroes may use ammunition." )
			//call AddFadingTextTag(plusammo,GetUnitX(u),GetUnitY(u),255,220,0,200)
		endif
		
		set u = null
	endfunction

	private function DropMagazinesConditions takes nothing returns boolean
		return GetSpellAbilityId()=='A009'
	endfunction

	private function DropMagazines takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local player p = GetOwningPlayer(u)
		local integer i = GetConvertedPlayerId(p)
		local integer inven = 0
		local integer n
		local item inv
		if (GetUnitTypeId(u)=='H005' and GetPrimaryAmmoMagazines(p)>=2) or GetPrimaryAmmoMagazines(p)>=3 then
			if udg_Reloading[i]!=2 and udg_Reloading[i]!=1 then
				loop
					exitwhen inven>5
					set inv = UnitItemInSlot(u,inven)
					set n = GetItemCharges(inv) 
					if GetItemTypeId(inv)=='shas' and n<8 then
						call SetItemCharges(inv,n+1)
						set inven=1337
					endif
					set inven=inven+1
				endloop
				if inven<1337 then
					set inven = 0
					loop
						exitwhen inven>5
						set inv = UnitItemInSlot(u,inven)
						set n = GetItemCharges(inv) 
						if GetItemTypeId(inv)=='I006' and n<3 then
							call SetItemCharges(inv,n+1)
							set inven=1337
						endif
						set inven=inven+1
					endloop
				endif
				set inv = null
				if inven<1337 then
					call UnitAddItemById(u,'I006')
				endif
				if GetUnitTypeId(u)=='H005' then
					call AddPrimaryAmmo(u, -2, 0)
				else
					call AddPrimaryAmmo(u, -3, 0)
				endif
				set bj_meleeNearestMineDist = GetUnitX(udg_Hero[i])
				set bj_lastTransmissionDuration = GetUnitY(udg_Hero[i])
				set udg_Sound3DVolume = 80
				set udg_soundpath = "war3mapImported\\clipout1.wav"
				call PlaySoundPath3D.execute()
				set udg_Sound3DVolume = 127
			else
				call IssueImmediateOrder(u,"holdposition")
				call DisplayTimedTextToPlayer( p, 0.0, 0.0, 2.0, "Cannot drop magazines while reloading." )
			endif
		else
			call IssueImmediateOrder(u,"holdposition")
			call DisplayTimedTextToPlayer( p, 0.0, 0.0, 2.0, "Insufficient Magazines." )
		endif
		set u = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ( t,  EVENT_PLAYER_UNIT_SPELL_CAST  )
		call TriggerAddCondition( t, Condition( function AcquireAmmoCondition ) )
		call TriggerAddAction(t,function AcquireAmmo)
		
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ( t,  EVENT_PLAYER_UNIT_SPELL_CAST  )
		call TriggerAddCondition(t, Condition(function DropMagazinesConditions) )
		call TriggerAddAction(t,function DropMagazines)
	endfunction

endlibrary
